package com.sunlands.zlcx.usercenter.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sunlands.zlcx.usercenter.domain.VipUserDO;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.beans.BeanUtils;

import javax.persistence.Column;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shixiaopeng
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@DynamicUpdate
@DynamicInsert
public class VipUserVO implements Serializable {

    private static final long serialVersionUID = -5211975848079627189L;

    @ApiModelProperty(value = "id")
    private Long id;

    /**
     * 用户表tz_user的id
     */
    @ApiModelProperty(value = "用户表的id")
    private Long userId;

    /**
     * 过期日期
     */
    @ApiModelProperty(value = "过期日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date expireDate;

    /**
     * 有效期
     */
    @ApiModelProperty(value = "有效期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date validDate;

    @ApiModelProperty(value = "当月过期日期")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date currExpireDate;

    @ApiModelProperty(value = "当月剩余天数")
    private Long currRemainDays;

    @ApiModelProperty(value = "预扣时长(用于切换扣费产品时使用)")
    private Long preUsedTime;

    /**
     * 使用时长
     */
    @ApiModelProperty(value = "当月已使用时长,分钟")
    private Long useredTime;

    /**
     * 总时长
     */
    @ApiModelProperty(value = "当月总时长，分钟")
    private Long totalTime;

    /**
     * 状态：1有效，0无效
     */
    @ApiModelProperty(value = "状态：1有效，0无效")
    private Integer status;

    /**
     * VIP用户等级
     * 200, "临时7天VIP"
     * 180, "加油小时卡"
     * 100, "普通VIP"
     * 150，免费时长
     */
    @ApiModelProperty(value = "200-临时7天VIP，180-加油小时卡，150-有免费时长，100-普通VIP, 10-体验")
    private Integer level;

    @ApiModelProperty(value="每天总的免费次数")
    private Integer totalFreeNum;
    /**
     *每天免费剩余次数
     */
    @ApiModelProperty(value="每天免费剩余次数")
    private Integer freeNum;

    @ApiModelProperty(value="每次免费分钟数")
    private Integer freeMinutesEveryTime;

    @ApiModelProperty(value="剩余恢复秒，针对月、季、年卡，冲刺卡")
    private Long surplusRecoverySecond;

    @ApiModelProperty(value = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(insertable = false, updatable = false)
    Date updateTime;

    @ApiModelProperty(value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(insertable = false, updatable = false)
    Date createTime;


    public static VipUserVO fromDO(VipUserDO vipUserDO) {
        if (Objects.isNull(vipUserDO)) {
            return null;
        }
        VipUserVO vipUserDTO = new VipUserVO();
        BeanUtils.copyProperties(vipUserDO, vipUserDTO);

        if (vipUserDO.getCurrExpireDate() != null) {
            LocalDate today = LocalDate.now();
            long daysDiff = ChronoUnit.DAYS.between(today, vipUserDO.getCurrExpireDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
            vipUserDTO.setCurrRemainDays(daysDiff);
        }
        return vipUserDTO;
    }

    public static List<VipUserVO> fromDO(List<VipUserDO> vipUserDOList) {
        if (Objects.isNull(vipUserDOList)) {
            return Collections.emptyList();
        }

        return vipUserDOList.stream()
                .map(VipUserVO::fromDO)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}
